package com.yty.order.controller;

import com.yty.order.entity.OrderDetail;
import com.yty.order.entity.OrderMaster;
import com.yty.product.apidata.OrderDetailInput;
import com.yty.product.client.ProductClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("test")
@RefreshScope
public class TestController {
    @Autowired
    private ProductClient productClient;

    @Resource
    private AmqpTemplate orderRabbitTemplate;
    @Resource
    private AmqpTemplate rabbitTemplate;
    @Resource
    private AmqpTemplate fanoutRabbitTemplate;

    @Value("${env}")
    private String env;

    @GetMapping("msg")
    public String msg() {
        return productClient.msg();
    }

    @GetMapping("env")
    public String env() {
        return env;
    }

    @GetMapping("sendObjectMessage")
    public void sendObjectMessage() {
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setBuyerName("杨天尧");
        orderRabbitTemplate.convertAndSend(orderMaster);
        orderRabbitTemplate.convertAndSend("杨天尧" + new Date());
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId("213123");
        orderRabbitTemplate.convertAndSend(orderDetail);
    }

    @GetMapping("sendSimpleMessage")
    public void sendSimpleMessage() {
        rabbitTemplate.convertAndSend("order", "test1", "杨天尧");
    }

    @GetMapping("sendFanoutMessage")
    public void sendFanoutMessage() {
        OrderDetailInput orderDetailInput = new OrderDetailInput();
        orderDetailInput.setProductId("123");
        orderDetailInput.setProductQuantity(10);
        fanoutRabbitTemplate.convertAndSend(orderDetailInput);
    }
}
